function love.load()
    -- our tiles (this will be removed once we decide on the background
    tile = {}
    for i=0,3 do
        tile[i] = love.graphics.newImage( "tile"..i..".png" )
    end
    
    -- the map (also to be removed)
    map={
    { 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0}, 
    { 3, 1, 0, 0, 2, 2, 2, 0, 3, 0, 3, 0, 1, 1, 1, 0, 0, 3, 0, 0, 0},
    { 3, 1, 0, 0, 2, 0, 2, 0, 3, 0, 3, 0, 1, 0, 0, 0, 0, 0, 3, 0, 0},
    { 3, 1, 1, 0, 2, 2, 2, 0, 0, 3, 0, 0, 1, 1, 0, 0, 0, 0, 0, 3, 0},
    { 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 3},
    { 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 2},
    { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    { 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    { 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    { 0, 2, 2, 2, 0, 3, 3, 3, 0, 1, 1, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0},
    { 0, 2, 0, 0, 0, 3, 0, 3, 0, 1, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 1},
    { 0, 2, 0, 0, 0, 3, 0, 3, 0, 1, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0},
    { 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    -- Player position (eventually)
    posX = 0
    posY = 0
    
    map_display_buffer = 1
    
    -- How many tiles to display to the screen;
    -- will correspond to the screen size
    map_display_w = 15
    map_display_h = 10
    
    tile_w = 64
    tile_h = 64
    
    -- Initialize the player avatar
    loadPlayer()
end

function loadPlayer()
	ball = {{400,300}}
	effect = love.graphics.newPixelEffect(([[

		#define NBALLS %d
		extern vec2[NBALLS] ball;

		float metaball(vec2 x)
		{
			x /= 20.0;
			return 1.0 / (dot(x, x) + .00001);
		}

		vec4 effect(vec4 color, Image tex, vec2 tc, vec2 pc)
		{
			float p = 0.0;
			for (int i = 0; i < NBALLS; ++i)
				p += metaball(pc - ball[i]);
			return vec4(1*p, 0*p, 0*p, 1*p);
		}
	]]):format(#ball))

	effect:send('ball', unpack(ball))
end
    
function drawMap()
    offset_x = posX % tile_w
    offset_y = posY % tile_h
    
    -- Top left tile on the player's screen
    firstTile_x = math.floor(posX / tile_w)
    firstTile_y = math.floor(posY / tile_h)
    
    for y=1, (map_display_h + map_display_buffer) do
        for x=1, (map_display_w + map_display_buffer) do
            -- Note that this condition block allows us to go beyond the edge of the map.
            if y+firstTile_y >= 1 and y+firstTile_y <= map_h
                and x+firstTile_x >= 1 and x+firstTile_x <= map_w
            then
                love.graphics.draw(
                    tile[map[y+firstTile_y][x+firstTile_x]], 
                    (x*tile_w) - offset_x - tile_w, 
                    (y*tile_h) - offset_y - tile_h)
            end
        end
    end
end

function love.update( dt )
    -- get input
    if love.keyboard.isDown( "up" ) then
        posY = posY-4
    end
    if love.keyboard.isDown( "down" ) then
        posY = posY+4
    end

    if love.keyboard.isDown( "left" ) then
        posX = posX-4
    end
    if love.keyboard.isDown( "right" ) then
        posX = posX+4
    end

    -- check boundaries
    if posX < 0 then
        posX = 0
    end

    if posY < 0 then
        posY = 0
    end 
 
    if posX > map_w * tile_w - map_display_w * tile_w then
        posX = map_w * tile_w - map_display_w * tile_w
    end
 
    if posY > map_h * tile_h - map_display_h * tile_h then
        posY = map_h * tile_h - map_display_h * tile_h
    end
end

function love.draw()
     drawMap()
	--love.graphics.setPixelEffect(effect)
     --love.graphics.draw(effect, 400, 300, 0, 1, 1, 400, 300)
end